package soccer.model.goals;

import java.util.List;

import soccer.utility.db.dbUtility;


public class GoalDAOImpl implements GoalDAO {
      public void insertGoal(Goal g){
    	  StringBuffer query = new StringBuffer();
    	  query.append("INSERT INTO ");
    	  query.append("(goalMinute, goalScorer , matchId) ");
    	  query.append(" VALUES (?,?,?)");
    	  
    	  Object [] params = new Object[]{g.getGoalMinute(),g.getGoalScorer(),g.getMatchId()};
    	  dbUtility db = dbUtility.getInstance();
    	  db.update(query.toString(), params);
      }
      public List getGoalsOfMatch(int matchId){
    	  List  matchGoals = null;
    	  StringBuffer query = new StringBuffer();
    	  query.append("SELECT * FROM ");
    	  query.append("goal ");
    	  query.append("WHERE matchId = ");
    	  query.append(matchId);
    	  dbUtility  db = dbUtility.getInstance();
    	  matchGoals = db.execute(query.toString());
    	  return matchGoals;
      }
      public List getGoalsOfPlayer(int playerId){
    	  List  matchGoals = null;
    	  StringBuffer query = new StringBuffer();
    	  query.append("SELECT * FROM ");
    	  query.append("goal ");
    	  query.append("WHERE goalScorer = ");
    	  query.append(playerId);
    	  dbUtility  db = dbUtility.getInstance();
    	  matchGoals = db.execute(query.toString());
    	  return matchGoals;
      }
     public List getMostGoalsNumberOfPlayer(){
    	 List mostScorers = null;
    	 StringBuffer query  = new StringBuffer();
    	 query.append("SELECT p.playerId ,p.name , p.surname, COUNT(g.goalId) AS goalNumber FROM ");
    	 query.append(" goal g , player p ");
    	 query.append("WHERE g.golScorer = p.playerId ");
    	 query.append(" GROUP BY p.playerId ORDER BY goalNumber DESC");
    	 
    	 dbUtility db = dbUtility.getInstance();
    	 mostScorers = db.execute(query.toString());
    	 return mostScorers;
     }
}
